1323A - Even Subset Sum Problem - CodeForces Solution


brute force dp greedy implementation *800

Please click on ads to support us..

Python Code:

import sys
input = lambda : sys.stdin.readline().strip()
'''
╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬        ▓▓█████▓▓      ╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
'''
for i in range(int(input())):
    l = int(input())
    arr = [int(i)%2 for i in input().split()]
    if arr.count(0)>0:
        print(1)
        print(arr.index(0)+1)
    elif l == 1:
        print(-1)
    else:
        print(2)
        print(l-1,l)



C++ Code:

///  بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ
///  اللهم صّلِ وسَلّمْ عَلي نَبِيْنَا مُحَمد



#define _CRT_SECURE_NO_WARNINGS
#define ThinkTwiceCodeOnce ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Num_of_Digits(n) ((int)log10(n) + 1)
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
using namespace std;


vector <int> v;
bool ans = 0;

void rec(int idx, ll sum, int sz, bool Add, queue <int> q)
{

    if (ans)
    {
        return;
    }

    if (Add)
    {
        q.push(idx);
    }

    if (idx >= v.size())
    {
        if (sum % 2 == 0 && sum != 0)
        {
            ans = 1;
            cout << sz << endl;
            while (q.size())
            {
                cout << q.front() << " ";
                q.pop();
            }

            cout << endl;
        
        }

        return;
    
    }

    else if (sum % 2 == 0 && sum != 0)
    {
        ans = 1;
        cout << sz << endl;
        while (q.size())
        {
            cout << q.front() << " ";
            q.pop();
        }

        cout << endl;

        return;
    }

    rec(idx + 1, sum + v[idx], sz + 1, true, q);
    rec(idx + 1, sum, sz, false, q);

}


int main()
{

    ThinkTwiceCodeOnce;

    //     Think Twice Code once!!!!!!     \\
    

    int tc;
    cin >> tc;

    while (tc--)
    {
        v.clear();
        int n;
        cin >> n;        
        
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            v.push_back(x);
        }

        ans = 0;

        queue <int> q;
        
        while (q.size())
        {
            q.pop();
        }

        rec(0, 0, 0, 0, q);
        
        if (ans == 0)
        {
            cout << -1 << endl;
        }

    }

    


}


Comments

Submit
0 Comments
More Questions

Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal